package com.example.test.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.domain.AbstractPageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@EqualsAndHashCode(callSuper = false)
@Data
public class PageParam extends AbstractPageRequest {
    private int page;
    private int size;
    private Sort.Direction direction;
    private String[] sort;

    @SuppressWarnings("WeakerAccess")
    public PageParam(int page, int size, Sort.Direction direction, String[] sort) {
        super(page, size);
        this.page = page;
        this.size = size;
        this.direction = direction;
        this.sort = sort;
    }

    public PageParam() {
        super(0, 20);
    }

    @Override
    public Sort getSort() {
        if(direction == null || sort == null || sort.length < 1){
            return Sort.unsorted();
        }
        return new Sort(direction, sort);
    }

    @Override
    public Pageable next() {
        return new PageParam(page + 1, size, direction, sort);
    }

    @Override
    public Pageable previous() {
        return page == 0 ? this : new PageParam(page + 1, size, direction, sort);
    }

    @Override
    public Pageable first() {
        return new PageParam(0, size, direction, sort);
    }
}
